import { Color, Event, Material, defined, Property } from "cesium";
type MaterialCache = {
  /**
   * 场景中的着色器对象
   */
  _materials: Object;
  /**
   * 添加着色器对象
   */
  addMaterial: (type: string, materialTemplate: Object) => void;
  /**
   * 获取着色器对象
   */
  getMaterial: (type: string) => void;
};
const MaterialCache: MaterialCache = (Material as any)._materialCache;

export class DynamicCircleDiffuseMaterial {
  _definitionChanged: any = new Event();
  get isConstant() {
    return false;
  }

  color = Color.WHEAT;
  _color: any = undefined;
  _colorSubscription: any = undefined;
  duration = 3000;
  _d: any = 8;
  _time: any = 0.0;
  constructor(options: { color: string; duration: number }) {
    this._definitionChanged = new Event();
    this._color = undefined;
    this._colorSubscription = undefined;
    this._time = new Date().getTime();
    this.color = Color.fromCssColorString(options.color || "red");
    this.duration = options.duration;
  }
  isTranslucent() {
    return true;
  }
  getType() {
    return "CircleDiffuseMaterial";
  }
  getValue(time: any, result: any) {
    if (!defined(result)) {
      result = {};
    }
    result.color = (Property as any).getValueOrClonedDefault(
      this._color,
      time,
      this.color,
      result.color
    );
    this._time += 0.01 / 2;
    result.time = this._time % 1000;
    // result.time =
    //   ((new Date().getTime() - this._time) % this.duration) / this.duration;

    return result;
  }
  equals(other: any) {
    return (
      this === other ||
      (other instanceof DynamicCircleDiffuseMaterial &&
        (Property as any).equals(this._color, other._color))
    );
  }
  get definitionChanged() {
    return this._definitionChanged;
  }
}

const source_ = `
  // czm_material czm_getMaterial(czm_materialInput materialInput)
  // {
  //   czm_material material = czm_getDefaultMaterial(materialInput);
  //   material.diffuse = 1.5 * color.rgb;
  //   vec2 st = materialInput.st;
  //   float dis = distance(st, vec2(0.5, 0.5));
  //   float per = fract(time);
  //   if(dis > per * 0.5){
  //     material.alpha = 0.0;
  //     discard;
  //   }else {
  //     material.alpha = color.a  * dis / per / 1.0;
  //   }
  //   return material;
  // }
// 伸缩圆环 
//   czm_material czm_getMaterial(czm_materialInput materialInput)
// {
//     czm_material material = czm_getDefaultMaterial(materialInput);
//     material.diffuse = 1.5 * color.rgb;
    
//     vec2 st = materialInput.st;
//     float dis = distance(st, vec2(0.5, 0.5));
    
//     // 使用 time 控制圆的周期性扩散
//     float per = fract(time);
//     float wave = sin(time * 3.14159 * 2.0); // 使用 sin 函数创建周期性波形
//     wave = abs(wave); // 取绝对值使波形保持正值
//     float radius = per * 0.5 * wave; // 根据波形控制圆的半径
    
//     // 控制多个圆的扩散效果
//     float fade = smoothstep(radius - 0.01, radius, dis) * smoothstep(radius + 0.01, radius, dis);
    
//     if (dis > radius) {
//         material.alpha = 0.0;
//         discard;
//     } else {
//         material.alpha = color.a * fade;
//     }
    
//     return material;
// }
// 动态扩散园
// czm_material czm_getMaterial(czm_materialInput materialInput)
// {
//     czm_material material = czm_getDefaultMaterial(materialInput);
//     material.diffuse = color.rgb;
    
//     vec2 st = materialInput.st;
//     float dis = distance(st, vec2(0.5, 0.5));
    
//     float speed = 0.05; // 控制波纹扩散的速度
//     float frequency = 3.0; // 控制波纹的频率
//     float amplitude = 0.1; // 控制波纹的宽度
//     float timeFactor = fract(czm_frameNumber * speed / 60.0);
    
//     float wave = sin((dis - timeFactor) * frequency * 2.0 * 3.14159) * 0.5 + 0.5;
//     float mask = smoothstep(0.0, amplitude, wave) * smoothstep(0.0, amplitude, 1.0 - wave);
    
//     material.alpha = mask * color.a;
    
//     return material;
// }
// 动态渐变园
// czm_material czm_getMaterial(czm_materialInput materialInput)
// {
//     czm_material material = czm_getDefaultMaterial(materialInput);
//     material.diffuse = color.rgb;
    
//     vec2 st = materialInput.st;
//     float dis = distance(st, vec2(0.5, 0.5));
    
//     float speed = 0.05; // 控制波纹扩散的速度
//     float frequency = 3.0; // 控制波纹的频率
//     float amplitude = 0.05; // 控制波纹的宽度
//     float timeFactor = fract(czm_frameNumber * speed / 60.0);
    
//     // 使用 sin 函数生成波纹
//     float wave = sin((dis - timeFactor) * frequency * 2.0 * 3.14159) * 0.5 + 0.5;
    
//     // 创建柔和边缘效果
//     float mask = smoothstep(0.0, amplitude, wave) * smoothstep(0.0, amplitude, 1.0 - wave);
    
//     // 根据距离 dis 设置透明度渐变，从外到里透明
//     float gradient = smoothstep(0.0, 1.0, dis);
    
//     material.alpha = mask * gradient * color.a;
    
//     return material;
// }
// 扩散多层渐变园
czm_material czm_getMaterial(czm_materialInput materialInput)
{
    czm_material material = czm_getDefaultMaterial(materialInput);
    material.diffuse = color.rgb;
    
    vec2 st = materialInput.st;
    float dis = distance(st, vec2(0.5, 0.5));
    
    float speed = 0.15; // 控制波纹扩散的速度
    float segments = 32.0; // 控制波纹的等分数量
    float amplitude = 0.15; // 控制波纹的宽度
    float timeFactor = czm_frameNumber * speed / 60.0;
    
    // 计算时间因子影响下的波纹位置
    float adjustedDis = (dis - timeFactor) * segments;
    float segmentFraction = fract(adjustedDis);
    
    // 创建柔和边缘效果
    float mask = smoothstep(0.0, amplitude, segmentFraction) * smoothstep(0.0, amplitude, 1.0 - segmentFraction);
    
    // 根据每个波纹内的距离设置透明度渐变，从外到里透明
    float gradient = smoothstep(0.0, 1.0, segmentFraction);
    
    // material.alpha = mask * gradient * color.a;
    material.alpha =  gradient * color.a;
    
    return material;
}


// 扇形扫描
// #define PI 3.141592653589793
// czm_material czm_getMaterial(czm_materialInput materialInput)
// {
//     czm_material material = czm_getDefaultMaterial(materialInput);
//     material.diffuse = color.rgb;

//     vec2 st = materialInput.st;
//     float dis = distance(st, vec2(0.5, 0.5));

//     // 设置扫描速度
//     float speed = 0.5;
//     float timeFactor = fract(czm_frameNumber * speed / 120.0);

//     // 计算当前扫描角度
//     float angle = atan(st.y - 0.5, st.x - 0.5);
//     if (angle < 0.0) {
//         angle += 2.0 * PI; // 使用内置的 PI 常量
//     }
//     angle /= 2.0 * PI; // 将角度转换为 [0, 1] 范围

//     // 控制扫描扇形范围
//     float scanWidth = 0.4; // 扇形宽度

//     // 计算扇形的起始角度和结束角度
//     float startAngle = timeFactor - scanWidth / 2.0;
//     float endAngle = timeFactor + scanWidth / 2.0;

//     // 确保角度在 [0, 1] 范围内
//     startAngle = mod(startAngle, 1.0);
//     endAngle = mod(endAngle, 1.0);

//     bool isInSector = false;

//     // 确定当前像素是否在扫描扇形内
//     if (startAngle < endAngle) {
//         isInSector = (angle >= startAngle) && (angle <= endAngle);
//     } else {
//         isInSector = (angle >= startAngle) || (angle <= endAngle);
//     }

//     // 计算角度渐变
//     float angularGradient = 0.0;
//     if (isInSector) {
//         if (startAngle < endAngle) {
//             angularGradient = (angle - startAngle) / (endAngle - startAngle);
//         } else {
//             angularGradient = (angle >= startAngle) ? 
//               (angle - startAngle) / (1.0 - startAngle + endAngle) : 
//               (angle +(1.0-startAngle))  / scanWidth;


//         }
//         angularGradient = clamp(angularGradient, 0.0, 1.0);
//     }

//     // 计算最终透明度
//     float opacity = isInSector ? angularGradient * color.a : 0.0;

//     // 添加外围纯色圆圈
//     float circleBorder = smoothstep(0.48, 0.49, dis) * smoothstep(0.51, 0.50, dis);

//     // 设置最终透明度
//     material.alpha = max(opacity, circleBorder);

//     return material;
// }



`;
MaterialCache.addMaterial("CircleDiffuseMaterial", {
  fabric: {
    type: "CircleDiffuseMaterial",
    uniforms: {
      color: Color.fromCssColorString("#fff"),
      time: 0,
    },
    source: source_,
  },
  translucent: function () {
    return true;
  },
});
